package com.gm.file_uploader.controller;

import com.gm.file_uploader.entity.dto.Speech;
import com.gm.file_uploader.service.AliOssService;
import com.gm.file_uploader.utils.Result;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 阿里云对象存储服务接口[用户图片上传]
 */
@RestController
@RequestMapping("/oss")
public class OssController {
  @Autowired
  private AliOssService oss;

  @RequestMapping(value = "/OSSupload",method = RequestMethod.POST)
  public Result<?> OssUpload(@RequestBody Speech s) throws IOException{
    File file = null;
    String filePath="D:\\Allproject\\record_question_project_1\\public\\sound\\download\\";
    File dir = new File(filePath);
    if (!dir.exists() && !dir.isDirectory()) {
      dir.mkdirs();
    }
    BufferedOutputStream bos = null;

    java.io.FileOutputStream fos = null;

    try {

      byte[] bytes = Base64.getDecoder().decode(s.getS());

      file=new File(filePath + s.getFileName());

      fos = new java.io.FileOutputStream(file);

      bos = new BufferedOutputStream(fos);

      bos.write(bytes);

    } catch (Exception e) {

      e.printStackTrace();

    } finally {

      if (bos != null) {

        try {

          bos.close();

        } catch (IOException e) {

          e.printStackTrace();

        }

      }

      if (fos != null) {

        try {

          fos.close();

        } catch (IOException e) {

          e.printStackTrace();

        }

      }
//    String upload=oss.upload(file);
//    return new Result<>(upload);
    return new Result<>("上传成功",s);
  }

}
}
